<template>
  <div>
     <el-form ref="form" :model="section" label-width="80px">
        <el-form-item label="课程名称">
            <el-input v-model="section.courseName" disabled></el-input>
        </el-form-item>
        <el-form-item label="章节名称">
            <el-input v-model="section.sectionName"></el-input>
        </el-form-item>
        <el-form-item label="章节描述">
            <el-input v-model="section.description"></el-input>
        </el-form-item>
         <el-form-item label="章节排序">
            <el-input v-model="section.orderNum">
                <template slot="append">数字控制排序，数字越大越靠后</template>
            </el-input>
        </el-form-item>
     </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancle">取 消</el-button>
            <el-button @click="submit" type="primary">确定</el-button>
        </span>
  </div>
</template>

<script>
import { saveOrUpdateSection, getBySectionId } from '@/services/course-section.js'
// import { getCourseById } from '@/services/course.js'

export default {
  props: {
    isEditor: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [Number, String]
    },
    courseName: {
      type: String
    },
    sectionId: {
      type: [Number, String]
    }
  },
  data () {
    return {
      section: {
        courseId: 0,
        courseName: '',
        sectionName: '',
        description: '',
        orderNum: 0,
        status: 0
      },
      course: {}
    }
  },
  methods: {
    cancle () {
      console.log(123)
      this.$emit('handleClose', 0)
    },
    async submit () {
      this.section.courseId = this.courseId
      const { data } = await saveOrUpdateSection(this.section)
      console.log(data, '================')
      this.$emit('handleClose', 1)
      this.section = {
        courseId: this.courseId,
        courseName: '',
        sectionName: '',
        description: '',
        orderNum: 0,
        status: 0
      }
      this.$message.success(this.isEditor ? '保存成功' : '添加成功')
    },
    loadCourseData () {
      this.section.courseName = this.courseName
    },
    async loadSection () {
      const { data } = await getBySectionId(this.sectionId)
      console.log('data', data)
      this.section = data.data
      this.section.courseName = this.courseName
    }
  },
  created () {
    console.log('this.isEditor======', this.isEditor)
    this.loadCourseData()
    if (this.isEditor) {
      this.loadSection()
    }
  },
  watch: {
    sectionId (newvalue, oldValue) {
      console.log('修改')
      this.loadSection()
    },
    isEditor (newvalue, oldValue) {
      if (!newvalue) {
        this.section = {
          courseId: this.courseId,
          courseName: this.courseName,
          sectionName: '',
          description: '',
          orderNum: 0,
          status: 0
        }
      }
    }
  }
};
</script>

<style>
.dialog-footer{
  display: block;
  text-align: right;
}
</style>
